define([
    "UtilDir/util",
    "UtilDir/tool",
    "UtilDir/searchBlock",
    "static/app/fwsjcj/rstatistical/rstatisticalEcharts",
    "EasyUI", "EasyUI-lang"
], function (Util, Tool, SearchBlock, Echarts) {

    var sysPath = getServer() + "/static/app/fwsjcj";


    /**
     * 页面初始化
     */
    var init = function () {
        initSearchBlock();
        queryBtnBind();
        createProvince();
        createstate();
        createProjectStateNumEcharts();
        clickTabs();
        window.addEventListener("resize", function () {
            var tabcontent_active = $('.tab-content').children('div.active');
            var tabcontent_div = tabcontent_active.children('div');

            for (var i = 0; i < tabcontent_div.length; i++) {
                var echarts_id = $(tabcontent_div[i]).attr('id');
                if (echarts_id) {
                    Echarts.resizeEcharts(echarts_id);
                }
            }
        })
    };
    var initSearchBlock = function () {
        SearchBlock.init("searchBlock");
        require(["jquery", "Date", "DateCN", "css!DateCss"], function ($) {
            $("#complex").datetimepicker({
                //设置使用语言：cn是自定义的中文版本，还可以扩展其他语言版本
                language: "cn",
                //输出格式化
                format: 'yyyy-mm-dd',
                //直接选择‘今天’
                todayBtn: true,
                //设置最精确的时间选择视图
                minView: 'month',
                //高亮当天日期
                todayHighlight: true,
                //选择完毕后自动关闭
                autoclose: true
            });
            /* 删除按钮 */
            $("#complex_show").on("click", function () {
                $('#complex').datetimepicker('show');
            });
            /* 显示按钮 */
            $("#complex_del").on("click", function () {
                $("#complex").val("");
            });
        })
    };
    var queryBtnBind = function () {
    	$("#collectionDate").css('display','none');
        $("#btnSearch").click(function () {
            debugger;
            // 请求参数
            var paramVo = {
                province: '',
                city: '',
                area: '',
                status: ''
            }
            // 参数赋值
            paramVo.province = $('#province').combobox('getText');
            if ($('#city').combobox('getText') != "全部") {
                paramVo.city = $('#city').combobox('getText');
            }
            if ($('#county').combobox('getText') != "全部") {
                paramVo.area = $('#county').combobox('getText');
            }
            paramVo.status = $('#state').combobox('getText');
            // 传参
            var tabid = $('#myTab').find("li.active a").attr('id');
            if (tabid == 'projectStatelab') {            
                createProjectStateNumEcharts(paramVo);
            } else if (tabid == 'projectTypelab') {            
                createProjectTypeEcharts(paramVo);
            } else if (tabid == 'projectAgelab') {            	
                createProjectAgeEcharts(paramVo);
            } else if (tabid == 'dailyCollectionlab') {            	
                createEarthquakeEcharts(paramVo);
            }

        });
        $("#btnReset").click(function () {
            $('#province').combobox('clear');
            $('#city').combobox('loadData', {});//清空option选项
            $('#city').combobox('clear');
            $('#county').combobox('loadData', {});//清空option选项
            $('#county').combobox('clear');
            $("#state").combobox("clear");
        });
    };

    var clickTabs = function () {
        $('#myTab').on('shown.bs.tab', function (e) {
            var tabid = $(e.target).attr('id');
            if (tabid == 'projectStatelab') {
            	$("#status").css('display','block');
            	$("#collectionDate").css('display','none');
                createProjectStateNumEcharts();
            } else if (tabid == 'projectTypelab') {
            	$("#status").css('display','block');
            	$("#collectionDate").css('display','none');
                createProjectTypeEcharts();
            } else if (tabid == 'projectAgelab') {
            	$("#status").css('display','block');
            	$("#collectionDate").css('display','none');
                createProjectAgeEcharts();
            } else if (tabid == 'dailyCollectionlab') {
            	$("#status").css('display','none');
            	$("#collectionDate").css('display','block');
                createEarthquakeEcharts();
            }

        })
    }

    var createProvince = function () {
        $('#province').combobox({
            url: "/app/divisions/root/subdivisions",
            method: 'get',
            editable: false, //不可编辑状态  
            cache: false,
            // panelHeight: 'auto',//自动高度适合  
            valueField: 'divisionId',
            textField: 'divisionName',
            onLoadSuccess: function (data) {
                $('#province').combobox('select', '');
            }
        });
        $("#province").combobox({
            onSelect: function (value) {
                var re = value.divisionId;
                var list = new Array();
                list.push({"divisionId": "-1", "divisionName": "全部"});
                if (re != null && re != '') {
                    list.push(re);
                }

                if (value.divisionName == '北京市' || value.divisionName == '天津市' || value.divisionName == '上海市' || value.divisionName == '重庆市') {
                    var division = [];
                    division.push(value);
                    $('#city').combobox('loadData', division);
                    $('#city').combobox('select', value.divisionName);

                    $('#county').combobox({
                        url: "/app/divisions/" + value.divisionId + "/subdivisions",
                        method: 'get',
                        editable: false, //不可编辑状态  
                        cache: false,
                        //panelHeight: 'auto',//自动高度适合  
                        valueField: 'divisionId',
                        textField: 'divisionName',
                        onLoadSuccess: function () {
                            $('#county').combobox('select', list[0].divisionName);
                        }
                    });
                } else {
                    $('#city').combobox({
                        url: "/app/divisions/" + value.divisionId + "/subdivisions",
                        method: 'get',
                        editable: false, //不可编辑状态  
                        cache: false,
                        // panelHeight: 'auto',//自动高度适合  
                        valueField: 'divisionId',
                        textField: 'divisionName',
                        onLoadSuccess: function () {
                            $('#city').combobox('select', list[0].divisionName);
                        }
                    });
                    createCity();
                    $('#county').combobox('loadData', {});//清空option选项
                    $('#county').combobox('clear');
                }
            }
        });

        createCity();
        $('#county').combobox({
            data: '',
            editable: false, //不可编辑状态  
            cache: false
        });
    }

    var createCity = function () {
        $("#city").combobox({
            onSelect: function (value) {
                var re = value.divisionId;
                var list = new Array();
                list.push({"divisionId": "-1", "divisionName": "全部"});
                if (re.list != null && re.list != 'null') {
                    list.push(re);
                }
                $('#county').combobox({
                    url: "/app/divisions/" + value.divisionId + "/subdivisions",
                    method: 'get',
                    editable: false, //不可编辑状态  
                    cache: false,
                    //panelHeight: 'auto',//自动高度适合  
                    valueField: 'divisionId',
                    textField: 'divisionName',
                    onLoadSuccess: function () {
                        $('#county').combobox('select', list[0].divisionName);
                    }
                });
            }
        });
    }

    var createstate = function () {
        var objectList = [{id: 1, text: '审核通过'}, {id: 2, text: '未审核'}]

        $('#state').combobox({
            data: objectList,
            editable: false, //不可编辑状态  
            cache: false,
            // panelHeight: 'auto',//自动高度适合  
            valueField: 'id',
            textField: 'text',
            onLoadSuccess: function (data) {
                // $('#province').combobox('select', data[0].divisionId);
            }
        });
    }

    /* 工程状态数量echarts */
    var createProjectStateNumEcharts = function (params) {
        console.log(params);

        /* 工程状态数量统计 */
        $.ajax({
            url: getServer() + "/fw/reinforce/projectBarNum",
            contentType: "application/json",
            method: "GET",
            data: params,
            success: function (data) {
                var buildings;
                buildings = data.data;
				console.log(buildings)
                Echarts.createBarEcharts({
                    id: "projectStateNumBar",
                    x: buildings.x,
                    yname: '数量',
                    title: "工程状态数量统计",
                    series: [{
                        title: '个数',
                        type: 'bar',
                        data: buildings.y,
                        force: {
                            repulsion: 300,
                            gravity: 0.003,
                            edgeLength: 140
                        }
                    }]
                });
                Echarts.resizeEcharts('projectStateNumBar');
            }
        });

        /* 工程状态数量占比 */
        $.ajax({
            url: getServer() + "/fw/reinforce/projectPieNum",
            contentType: "application/json",
            method: "GET",
            data: params,
            success: function (data) {
                console.log(data);

                Echarts.createPieEcharts({
                    id: "projectStateNumPie",
                    legenddata: ['加固工程', '新建工程'],
                    title: "工程状态数量占比",
//                  subtitle: '城市',
                    series: [
                        {
                            name: '数量',
                            data: data.data
                        }
                    ]
                });
                Echarts.resizeEcharts('projectStateNumPie');
            }
        });

    }

    /* 工程类别统计echarts */
    var createProjectTypeEcharts = function (params) {

        $.ajax({
            url: getServer() + "/fw/reinforce/projectPieNumByCategory",
            contentType: "application/json",
            method: "GET",
            data: params,
            success: function (data) {
                console.log(data);

                Echarts.createPieEcharts({
                    id: "projectTypePie",
                    legenddata: ['城镇住宅', '大中小学校', '医院', '农村民居', '重要交通生命线', '电力网络', '电信网络', '水库大坝', '危险化学品厂库', '应急避难场所建设'],
                    title: "工程类别统计",
//                  subtitle: '乡村',
                    series: [
                        {
                            name: '数量',
                            data: data.data
                        },
                    ]
                });
                Echarts.resizeEcharts('projectTypePie');
            }
        });
    }

    /* 工程年代echarts */
    var createProjectAgeEcharts = function (params) {
        $.ajax({
            url: getServer() + "/fw/reinforce/reinforceyear",
            contentType: "application/json",
            method: "GET",
            data: params,
            success: function (data) {
                console.log(data);
                Echarts.createPieEcharts({
                    id: "projectAgePie",
                    legenddata: ['80s以前', '80s-90s', '90s-00s', '00s-10s', '10s以后','建设中（新增）'],
                    title: "工程年代",
                    subtitle: '',
                    series: [
                        {
                            name: '数量',
                            data: data.reinforceYear
                        }
                        
                    ]
                });
                Echarts.resizeEcharts('projectAgePie');
            }
        });
    }

    /* 抗震设防烈度占比统计echarts */
    var createEarthquakeEcharts = function (params) {
        $.ajax({
            url: getServer() + "/fw/analysis/fortificationIntensity",
            contentType: "application/json",
            method: "GET",
            data: params,
            success: function (data) {
                console.log(data);

                Echarts.createPieEcharts({
                    id: "earthquake",
                    legenddata: ['未设防', '6度', '7度', '8度', '9度', '10度'],
                    title: "抗震设防烈度占比统计",
                    subtitle: '',
                    series: [
                        {
                            name: '面积',
                            data: data.area
                        },
                        {
                            name: '栋数',
                            data: data.num
                        }
                    ]
                });
                Echarts.resizeEcharts('earthquake');
            }
        });
    }

    /* 抗震设防情况统计echarts */
    var createEarthquakeSituationEcharts = function (params) {
        $.ajax({
            url: getServer() + "/fw/analysis/result",
            contentType: "application/json",
            method: "GET",
            data: params,
            success: function (data) {
                console.log(data);

                Echarts.createPieEcharts({
                    id: "earthquakeSituation",
                    legenddata: ['未设防', '抗震设防能力不足', '基本符合抗震设防'],
                    title: "抗震设防情况统计",
                    subtitle: '',
                    series: [
                        {
                            name: '面积',
                            data: data.area
                        },
                        {
                            name: '栋数',
                            data: data.num
                        }
                    ]
                });
                Echarts.resizeEcharts('earthquakeSituation');
            }
        });
    }

    /* 农村居住房屋使用情况统计echarts */
    var createRuralHouseEcharts = function (params) {
        $.ajax({
            url: getServer() + "/fw/analysis/houseUseSituation",
            contentType: "application/json",
            method: "GET",
            data: params,
            success: function (data) {
                console.log(data);

                Echarts.createPieEcharts({
                    id: "ruralHouse",
                    legenddata: ['自用', '空置', '出租', '经营', '被征或待征', '其他'],
                    title: "农村居住房屋使用情况占比统计",
                    subtitle: '',
                    series: [
                        {
                            name: '面积',
                            data: data.area
                        },
                        {
                            name: '栋数',
                            data: data.num
                        }
                    ]
                });
                Echarts.resizeEcharts('ruralHouse');
            }
        });
    }



    return {
        init: init
    };
});
